package net.bolue.sysmng.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.bolue.console.model.BusiBase;
import net.bolue.sysmng.db.DbOpUtil;
import net.bolue.sysmng.exception.BolueException;
import net.bolue.sysmng.tblbean.ColInfo;
import net.bolue.web.report.RepConstants;

/**
 * 表列信息业务类
 * @author Administrator
 *
 */
public class ColInfoModel extends BusiBase{

	private List<ColInfo> colInfoList = null;
	private List<ColInfo> keyColInfoList = null;
	private HashMap<String,ColInfo> colInfoMap = null;
	
	public ColInfoModel() {
		super();
	}

	public ColInfoModel(HttpServletRequest request) {
		super(request);
	}

	/**
	 * 获得表列信息List格式
	 * @param subSys	子系统
	 * @param tblName	数据表名
	 * @return List		表列信息
	 * @throws BolueException
	 */
	 public List<ColInfo> getColInfoList(String subSys, String tblName) throws BolueException{
		 if(this.colInfoList!=null){
			 return this.colInfoList;
		 }
		 if ((subSys == null) || (tblName == null) || "".equals(tblName)){
			 throw new BolueException(5,"参数subSys及tblName均不能为null!");
		 }
		 DbOpUtil dbOpUtil = new DbOpUtil(RepConstants.SYSMNG_SUBSYS_KEY);
		 String sql = "SELECT * FROM st_colinfo WHERE SUBSYS=? and TBLNAME=? ORDER BY DISPNO";
		 Object[] paramters = new Object[]{subSys,tblName};
		 List<ColInfo> list = dbOpUtil.query(ColInfo.class, sql, paramters);
		 this.colInfoList = list;
		 return list;
	 }
	 
	 /**
	  * 获得主键表列信息
	  * @param subSys
	  * @param tblName
	  * @return
	  * @throws BolueException
	  */
	 public List<ColInfo> getKeyColInfoList(String subSys, String tblName) throws BolueException{
		 if(this.keyColInfoList!=null){
			 return this.keyColInfoList;
		 }
		 if(this.colInfoList==null&&this.colInfoList.size()<1){
			 tblName = tblName.substring(tblName.lastIndexOf(".") + 1);
			 getColInfoList(subSys, tblName);
		 }
		 if(this.colInfoList==null&&this.colInfoList.size()<1){
			 return null;
		 }
		 List<ColInfo> list = new ArrayList<ColInfo>();
		 for(int i=0;i<this.colInfoList.size();i++){
			 ColInfo colInfo = this.colInfoList.get(i);
			 if(colInfo.getIsPkey()==1){
				 list.add(colInfo);
			 }
		 }
		 
		 this.keyColInfoList = list;
		 return list;
	 }
	 
	 /**
	  * 获得表列信息Map格式
	  * @param subSys	子系统
	  * @param tblName	数据表名
	  * @return Map
	  * @throws BolueException
	  */
	 public Map<String,ColInfo> getColInfoMap(String subSys, String tblName)throws BolueException{
		 if(this.colInfoMap!=null){
			 return this.colInfoMap;
		 }
		 if(this.colInfoList==null||this.colInfoList.size()<1){
			 tblName = tblName.substring(tblName.lastIndexOf(".") + 1);
			 getColInfoList(subSys, tblName);
		 }
		 if(this.colInfoList==null&&this.colInfoList.size()<1){
			 return null;
		 }
		 HashMap<String,ColInfo> localHashMap = new HashMap<String,ColInfo>(colInfoList.size());
		 for(int i=0;i<this.colInfoList.size();i++){
			 ColInfo colInfo = this.colInfoList.get(i);
			 localHashMap.put(colInfo.getColName(), colInfo);
		 }
		 this.colInfoMap = localHashMap;
		 return localHashMap;
	 }
	 
//	 public void insertBatchTblInfos(String subSys, String[] selTbls) throws BolueException{
//		for (int i = 0; i < selTbls.length; i++) {
//			insertTblInfo(subSys,selTbls[i]);
//		}
//	 }
//	 
//	 public int insertTblInfo(String subSys, String selTbl) throws BolueException{
//		 TblInfoModel tblInfoModel = new TblInfoModel();
//		 TblInfo tblInfo = tblInfoModel.getTblInfo(subSys, selTbl);
//		 if(tblInfo==null){
//			 String tableName = selTbl.substring(selTbl.lastIndexOf(".") + 1);
//			 tblInfo = new TblInfo();
//			 tblInfo.setSubSys(subSys);
//			 tblInfo.setTblName(tableName);
//		 }
//	 }
//	 
//	 public List<ColInfo> getColInfo(String subSys, String selTbl) throws BolueException{
//		 List<ColInfo> colInfoList = getColInfoByMeta(subSys,selTbl);
//	 }
//	 
//	 public List<ColInfo> getColInfoByMeta(String subSys, String selTbl) throws BolueException{
//		 List<ColInfo> colInfoList = new ArrayList<ColInfo>();
//		 List<ColMetaData> colMetaList = DBMetaManager.getAllColumns(subSys, selTbl);
//		 Map<String,PKeyMetaData> pkMap = DBMetaManager.getPKeyColumnsMap(subSys, selTbl);
//		 if(colMetaList==null||colMetaList.size()<1){
//			 return null;
//		 }
//		 String tableName = selTbl.substring(selTbl.lastIndexOf(".") + 1);
//		 for (int j = 0; j < colMetaList.size(); j++){
//			 ColMetaData colMetaData = colMetaList.get(j);
//			 ColInfo colInfo = DbMetaUtil.metaToCol(colMetaData);
//			 if(colInfo==null){
//				 continue;
//			 }
//			 colInfo.setSubSys(subSys);
//			 colInfo.setTblName(tableName);
//			 if(pkMap!=null&&pkMap.get(colInfo.getColName())!=null){
//				 colInfo.setIsPkey(1);
//			 }
//			 colInfoList.add(colInfo);
//		 }
//		 return colInfoList;
//	 }
	 
	 /*
	 public int insertTblInfo(String subSys, String selTbl) throws BolueException{
		 String tableName = selTbl.substring(selTbl.lastIndexOf(".") + 1);
		 TblInfoModel tblInfoModel = new TblInfoModel();
		 TblInfo tblInfo = tblInfoModel.getTblInfo(subSys, selTbl);
		 if(tblInfo==null){
			 tblInfo = new TblInfo();
			 tblInfo.setSubSys(subSys);
			 tblInfo.setTblName(tableName);
		 }
		 List<ColInfo> colInfoList = getColInfo(subSys,selTbl);
		 DbOpUtil dbOpUtil = new DbOpUtil(RepConstants.SYSMNG_SUBSYS_KEY,true);
			Map<String,Object> whereMap = new HashMap<String,Object>();
			whereMap.put("SUBSYS", subSys);
			whereMap.put("TBLNAME", selTbl);
			dbOpUtil.delete("st_tblinfo", whereMap);
			int i = dbOpUtil.insert(tblInfo);
			dbOpUtil.delete("st_colinfo", whereMap);
			for(ColInfo colInfo : colInfoList){
				dbOpUtil.insert(colInfo);
			}
			dbOpUtil.commitTrans();
			return i;
		}
		*/
}
